const promise_state = {
    pending: 0
    , fulfilled: 1
    , rejected: 2
}
class newPromise {
    #result
    #state = promise_state.pending // pending 0 fulfilled 1 rejected 2
    #callback = []
    constructor(executor) {
        executor(this.#resolve, this.#reject)//调用回调函数
    }
    #resolve = (value) => {
        if (this.#state !== promise_state.pending) return
        this.#result = value
        this.#state = promise_state.fulfilled //表示数据填充成功了
        queueMicrotask(() => {
            this.#callback.forEach((cb) => {
                cb()
            })
        })
    }
    #reject(reason) { }
    then(onFulfilled, onRejected) {
        /* 
            解决无法链式代用的问题
        */
        return new newPromise((resolve, reject) => {
            if (this.#state === promise_state.pending) {

                this.#callback.push(() => {
                    resolve(onFulfilled(this.#result))
                })

            }
            else if (this.#state === promise_state.fulfilled) {
                queueMicrotask(() => { resolve(onFulfilled(this.#result)) })
            }
        })

    }

}
const mp = new newPromise((resolve, reject) => {
    setTimeout(() => {
        resolve("GG")
    }, 1000)
})
console.log(mp);
mp.then((result) => {
    console.log("读取数据", result);
    return result
}).then(r => {
    console.log("3", r);
})
